package com.liudada.cloud.aspect;

import com.liudada.cloud.annotation.SysLog;
import com.liudada.cloud.resp.ResultData;
import com.liudada.cloud.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author liushuntao
 * @date 2024/4/18
 */

@Aspect
@Component
@Slf4j
public class SysLogAop {

    @Pointcut("@annotation(com.liudada.cloud.annotation.SysLog)")
    public void setLog() {}

    @Around("setLog()")
    private Object setLogAfter(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("SysLogAop");
        //获取方法内容
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        SysLog annotation = method.getAnnotation(SysLog.class);
        String value = annotation.value();
        log.info("注解内容 = {}", value);
        if(checkAnnotation(value)){
            //执行方法
           return joinPoint.proceed();
        }
        return ResultData.fail(ReturnCodeEnum.RC401.getCode(), ReturnCodeEnum.RC401.getMessage());
    }

    private boolean checkAnnotation(String value) {
        return Boolean.TRUE;
    }

}
